package com.common.pager;

import java.util.Enumeration;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.common.util.StringUtil;

public class PageHelper {
	private String output; // 保存输出

	/**
	 * 总页数
	 * 
	 */
	private int pageCount = 0;

	/**
	 * 当前显示的页
	 */
	private int currentPage = 1;

	/**
	 * 总记录数量
	 * 
	 */
	private int totalCount = 0;

	/**
	 * 每页显示条数
	 */
	private int rowPage = 12;

	private HttpServletRequest request = null;

	private int begin = 0;
	private int end = 0;
	HashMap params = new HashMap();

	private String form="pageForm";
	private String requestUrl="delete";	//savePagerList
	private String pagerUrl="find";	//findPagerList
	String linkUrl = ""; // URL
	public PageHelper() {

	}
	public PageHelper(HttpServletRequest request) {
		this.request = request;
	}

	public void setUrl(String requestUrl,String pagerUrl){
		this.requestUrl=requestUrl;
		this.pagerUrl=pagerUrl;
	}
	/**
	 * 初始化分页
	 * 
	 * 
	 * @param totalCount
	 *            int 总条数
	 * 
	 * @param rowPage
	 *            int 每页显的日志数
	 * 
	 * @param request
	 *            HttpServletRequest
	 */
	private void init(int totalCount, int rowPage, HttpServletRequest request) {
		this.totalCount = totalCount;
		this.rowPage = rowPage;
		this.request = request;
		pageCount = totalCount % rowPage > 0 ? (totalCount / rowPage + 1)
				: (totalCount / rowPage);

		if (request.getParameter("currentPage") != null
				&& !"".equals(request.getParameter("currentPage"))&& !"null".equals(request.getParameter("currentPage"))) {
			this.currentPage = Integer.parseInt(request
					.getParameter("currentPage"));
		}
		if(currentPage>pageCount){
			currentPage=pageCount;
			
		}
		if (currentPage > 1) {
			this.begin = rowPage * (this.currentPage - 1);
		}
		linkUrl = StringUtil.getCurUri(request);
		if(!StringUtil.isBlank(requestUrl)&&!StringUtil.isBlank(pagerUrl)){
			linkUrl = linkUrl.replaceAll(requestUrl, pagerUrl);
		}
		String param="";
		if (linkUrl.indexOf("?") == -1) {
			// 不存在参数
			linkUrl += "?currentPage=";
			param="1";
		} else {
			// 存在参数
			int pos = linkUrl.indexOf("currentPage");
			if (pos == -1) {
				// 不存在分页参数
				linkUrl=linkUrl.substring(0,linkUrl.indexOf("?"));
				linkUrl += "?currentPage=";
				param="1";
			} else {
				// 存在分页参数
				linkUrl = linkUrl.substring(0, pos + 12);
				param=request.getParameter("currentPage");
				
			}
		}
		Enumeration enumeration;
		String name;
		String value;
		StringBuffer paramBuf = new StringBuffer();
		enumeration = request.getParameterNames();
		paramBuf.append("<form name='"+form+"' action='"+linkUrl+param+"' method='post'>");
		while (enumeration.hasMoreElements()) {
			name = (String) enumeration.nextElement();
			value = ParamUtil.getParameter(request, name);
			// 保存查询参数和值

			params.put(name, value);
			/*if (name.equals("currentPage")) {
				continue;
			}*/
			/*if (name.toLowerCase().equals("id")) {//删除数据时用的ID
				continue;
			}*/
			/*if (name.toLowerCase().equals("method")) {//删除数据时用的ID
				continue;
			}*/
			paramBuf.append("<input id='" + name + "' type='hidden' name='"
					+ name + "' value='" + value + "'>\n");
		}
		paramBuf.append("</form>");
		output = paramBuf.toString();
	}
	/**
	 * 王剑 2009-03-11
	 * @return
	 */
	public PagerVO paginate() {

		StringBuffer rt = new StringBuffer();
		
		rt.append("<SPAN class='current'>共有&nbsp;"+totalCount+"&nbsp;条记录");
		if(this.pageCount>0){
		rt.append(",&nbsp;当前第&nbsp;" + this.currentPage +"/"+this.pageCount + "&nbsp;页&nbsp;</span>");
		if (currentPage > 1 && pageCount > 1) {
			rt.append("<a class='list_link' href=\"");
			rt.append("#");
			rt.append("\" onclick=\"javascript:document." + form + ".action='"
					+ linkUrl+"1';document." + form
					+ ".submit();return false;\">");
		}
		rt.append("&lt;&lt;首页");
		if (currentPage > 1 && pageCount > 1) {
			rt.append("</a>&nbsp;");
		}
		if (currentPage > 1 && pageCount > 1) {
			rt.append("<a class='list_link' href=\"");
			rt.append("#");
			rt.append("\" onclick=\"javascript:document." + form + ".action='"
					+ linkUrl
					+ (this.currentPage - 1) + "';document." + form
					+ ".submit();return false;\">");
		}
		rt.append("&lt;上一页");
		if (currentPage > 1 && pageCount > 1) {
			rt.append("</a>&nbsp;");
		}
		if (currentPage < pageCount) {
			rt.append("<a class='list_link' href=\"");
			rt.append("#");
			rt.append("\" onclick=\"javascript:document." + form + ".action='"
					+ linkUrl
					+ (this.currentPage + 1) + "';document." + form
					+ ".submit();return false;\">");
		}
		rt.append("下一页&gt;");
		if (currentPage < pageCount) {
			rt.append("</a>&nbsp;");
		}
		if (currentPage < pageCount) {
			rt.append("<a class='list_link' href=\"");
			rt.append("#");
			rt.append("\" onclick=\"javascript:document." + form + ".action='"
					+ linkUrl
					+ this.pageCount + "';document." + form
					+ ".submit();return false;\">");
		}
		rt.append("尾页&gt;&gt;");
		if (currentPage < pageCount) {
			rt.append("</a>&nbsp;&nbsp;");
		}
		
		
		rt.append("转到<select id='pagerSelect' name='pagerSelect'  style=\"width:70;\" class=\"intext\");");
		rt.append("\" onchange=\"javascript:document." + form + ".action='"
				+ linkUrl + "'+this.value;document." + form
				+ ".submit();return false;\"");
		rt.append(">");
		for(int i=1;i<=pageCount;i++){
			if(i==currentPage){
				rt.append("<option value='"+i+"' selected='selected'>第"+i+"页</option>");
			}else{
				rt.append("<option value='"+i+"'>第"+i+"页</option>");
			}
		}
		rt.append("</select>");
		}
		PagerVO page = new PagerVO(rt.toString(),output);
		page.setCurrentPage(currentPage);
		page.setRowPage(rowPage);
		//String[] arg = { rt.toString(), output };
		return page;
	}
	/**
	 * 王剑 2009-03-11
	 * @return
	 */
	public String[] paginate6() {

		StringBuffer rt = new StringBuffer();
		
		rt.append("<SPAN class='current'>共&nbsp;"+pageCount+"页&nbsp;"+totalCount+"&nbsp;条数据&nbsp;&nbsp;第&nbsp;" + this.currentPage + "&nbsp;页&nbsp;</span>");
		if (currentPage > 1 && pageCount > 1) {
			rt.append("<a class='list_link' href=\"");
			rt.append("#");
			rt.append("\" onclick=\"javascript:document." + form + ".action='"
					+ linkUrl+"1';document." + form
					+ ".submit();return false;\">");
		}
		rt.append("&lt;&lt;首页");
		if (currentPage > 1 && pageCount > 1) {
			rt.append("</a>&nbsp;");
		}
		if (currentPage > 1 && pageCount > 1) {
			rt.append("<a class='list_link' href=\"");
			rt.append("#");
			rt.append("\" onclick=\"javascript:document." + form + ".action='"
					+ linkUrl
					+ (this.currentPage - 1) + "';document." + form
					+ ".submit();return false;\">");
		}
		rt.append("&lt;上一页");
		if (currentPage > 1 && pageCount > 1) {
			rt.append("</a>&nbsp;");
		}
		if (currentPage < pageCount) {
			rt.append("<a class='list_link' href=\"");
			rt.append("#");
			rt.append("\" onclick=\"javascript:document." + form + ".action='"
					+ linkUrl
					+ (this.currentPage + 1) + "';document." + form
					+ ".submit();return false;\">");
		}
		rt.append("下一页&gt;");
		if (currentPage < pageCount) {
			rt.append("</a>&nbsp;");
		}
		if (currentPage < pageCount) {
			rt.append("<a class='list_link' href=\"");
			rt.append("#");
			rt.append("\" onclick=\"javascript:document." + form + ".action='"
					+ linkUrl
					+ this.pageCount + "';document." + form
					+ ".submit();return false;\">");
		}
		rt.append("尾页&gt;&gt;");
		if (currentPage < pageCount) {
			rt.append("</a>&nbsp;&nbsp;");
		}
		rt.append("<select id='pagerSelect' name='pagerSelect'  style=\"width:70;\" class=\"intext\");");
		rt.append("\" onchange=\"javascript:document." + form + ".action='"
				+ linkUrl + "'+this.value;document." + form
				+ ".submit();return false;\"");
		rt.append(">");
		for(int i=1;i<=pageCount;i++){
			if(i==currentPage){
				rt.append("<option value='"+i+"' selected='selected'>第"+i+"页</option>");
			}else{
				rt.append("<option value='"+i+"'>第"+i+"页</option>");
			}
		}
		rt.append("</select>");
		rt.append("<script type=\"text/javascript\">");
		rt.append("function delData(ac){");
		rt.append("	if(confirm('确认删除?')){");
		rt.append("		document."+form+".action=ac;");
		rt.append("		document."+form+".submit();");
		rt.append("	}");
		rt.append("}");
		rt.append("</script>");
		
		String[] arg = { rt.toString(), output };
		return arg;
	}
	/**
	 * 王剑 2009-11-11
	 * @return
	 */
	public String[] paginate7() {
		
		StringBuffer rt = new StringBuffer();
		rt.append("<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">");
        rt.append("	<tr>");
        rt.append("		<td class=\"STYLE4\">");

        
		rt.append("共&nbsp;"+pageCount+"页&nbsp;"+totalCount+"&nbsp;条数据&nbsp;&nbsp;第&nbsp;" + this.currentPage + "&nbsp;页&nbsp;");
		if (currentPage > 1 && pageCount > 1) {
			rt.append("<a class='list_link' href=\"");
			rt.append("#");
			rt.append("\" onclick=\"javascript:document." + form + ".action='"
					+ linkUrl+"1';document." + form
					+ ".submit();return false;\">");
		}
		rt.append("&lt;&lt;首页");
		if (currentPage > 1 && pageCount > 1) {
			rt.append("</a>&nbsp;");
		}
		if (currentPage > 1 && pageCount > 1) {
			rt.append("<a class='list_link' href=\"");
			rt.append("#");
			rt.append("\" onclick=\"javascript:document." + form + ".action='"
					+ linkUrl
					+ (this.currentPage - 1) + "';document." + form
					+ ".submit();return false;\">");
		}
		rt.append("&lt;上一页");
		if (currentPage > 1 && pageCount > 1) {
			rt.append("</a>&nbsp;");
		}
		if (currentPage < pageCount) {
			rt.append("<a class='list_link' href=\"");
			rt.append("#");
			rt.append("\" onclick=\"javascript:document." + form + ".action='"
					+ linkUrl
					+ (this.currentPage + 1) + "';document." + form
					+ ".submit();return false;\">");
		}
		rt.append("下一页&gt;");
		if (currentPage < pageCount) {
			rt.append("</a>&nbsp;");
		}
		if (currentPage < pageCount) {
			rt.append("<a class='list_link' href=\"");
			rt.append("#");
			rt.append("\" onclick=\"javascript:document." + form + ".action='"
					+ linkUrl
					+ this.pageCount + "';document." + form
					+ ".submit();return false;\">");
		}
		rt.append("尾页&gt;&gt;");
		if (currentPage < pageCount) {
			rt.append("</a>&nbsp;&nbsp;");
		}
		
		String[] arg = { rt.toString(), output };
		return arg;
	}
	/**
	 * 
	 * @param totalCount
	 *            int
	 * @param rowPage
	 *            int
	 * @param request
	 *            HttpServletRequest
	 */
	public void setPages(int rowPage, HttpServletRequest request, String form) {
		this.rowPage = rowPage;
		this.request = request;
		this.form = form;
	}

	public int getBegin() {
		return begin;
	}

	public int getRowPage() {
		return rowPage;
	}

	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
		this.init(this.totalCount, this.rowPage, this.request);
	}

	public void setRowPage(int rowPage) {
		this.rowPage = rowPage;
	}

	public void setBegin(int begin) {
		this.begin = begin;
	}

	public String getParameter(String par) {
		return request.getParameter(par);
	}

	public HttpSession getSession() {
		return request.getSession();
	}

	public int getTotalCount() {
		return totalCount;
	}
	public int getCurrentPage() {
		return currentPage;
	}
	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}
	public int getIndexSer(){
		return (getCurrentPage()-1)*rowPage+1;
	}
	public int getEnd() {
		end = this.getRowPage()+this.getBegin();
		end = end>this.getTotalCount()?this.getTotalCount():end;
		return end;
	}
	public void setEnd(int end) {
		this.end = end;
	}
}
